#include "juzhen.h"

void Delay_ms(int xms)	//15单片机延时函数
{
	int i;
	long a;
	for (i = xms; i > 0; i --)
		for(a = 810; a > 0; a --);
}

unsigned char KeyJZ_16(void)
{
	unsigned char KeyNumber = 0;
	unsigned char temp;
	
	//第一列
	P3 = 0x7F;	//P37 = 0,其他是1，检测第一列
	P4 = 0xEF;	//P44 = 0; P42 = 1;	//15单片机第一列占用的引脚
	temp = P3;	//按键检测，输入
	temp &= 0x0F;
	if(temp != 0x0F)
	{
		Delay_ms(20);	//延时20ms来确认是否真正按下按键
		temp = P3;
		temp &= 0x0F;//检测是第一列哪个按键
		if(temp != 0x0F)
		{
			temp = P3;
			switch (temp)
			{
				case 0x7E : KeyNumber = 7; break;
				case 0x7D : KeyNumber = 6; break;
				case 0x7B : KeyNumber = 5; break;
				case 0x77 : KeyNumber = 4; break;
			}
			while(temp != 0x0f)
			{
				temp = P3;
				temp &= 0x0F;	//因为这条，所以松手检测才写成 temp != 0x0f
			}
		}
		
	}
	
	//第二列
	P3 = 0xBF;
	P4 = 0xFB;	//P42 = 0; P44 = 1;	//15单片机第二列占用的引脚	
	temp = P3;
	temp &= 0x0F;
	if(temp != 0x0F)
	{
		Delay_ms(20);
		temp = P3;
		temp &= 0x0F;
		if(temp != 0x0F)
		{
			temp = P3;
			switch (temp)
			{
				case 0xBE : KeyNumber = 11; break;
				case 0xBD : KeyNumber = 10; break;
				case 0xBB : KeyNumber = 9; break;
				case 0xB7 : KeyNumber = 8; break;
			}
			while(temp != 0x0f)
			{
				temp = P3;
				temp &= 0x0F;	//因为这条，所以松手检测才写成 temp != 0x0f
			}
		}	
	}
	
	//第三列
	P3 = 0xDF;	
	P4 = 0xFF;
	temp = P3;
	temp &= 0x0F;
	if(temp != 0x0F)
	{
		Delay_ms(20);
		temp = P3;
		temp &= 0x0F;
		if(temp != 0x0F)
		{
			temp = P3;
			switch (temp)
			{
				case 0xDE : KeyNumber = 15; break;
				case 0xDD : KeyNumber = 14; break;
				case 0xDB : KeyNumber = 13; break;
				case 0xD7 : KeyNumber = 12; break;
			}
			while(temp != 0x0f)
			{
				temp = P3;
				temp &= 0x0F;	//因为这条，所以松手检测才写成 temp != 0x0f
			}
		}	
	}	
	
	//第四列
	P3 = 0xEF;	
	temp = P3;
	temp &= 0x0F;
	if(temp != 0x0F)
	{
		Delay_ms(20);
		temp = P3;
		temp &= 0x0F;
		if(temp != 0x0F)
		{
			temp = P3;
			switch (temp)
			{
				case 0xEE : KeyNumber = 19; break;
				case 0xED : KeyNumber = 18; break;
				case 0xEB : KeyNumber = 17; break;
				case 0xE7 : KeyNumber = 16; break;
			}
			while(temp != 0x0f)
			{
				temp = P3;
				temp &= 0x0F;	//因为这条，所以松手检测才写成 temp != 0x0f
			}
		}	
	}
	
	return KeyNumber;
}
